const express = require('express')
const path = require('path')
// const {imageDir,openUrl,port} = require("../server.config.js")
const {log} = require('../utils/logger.js')
const goodDao = require('../dao/goodDao.js')

const goodRounter = express.Router()

//根据id获取商品详情页的介绍图片
goodRounter.get('/getDetailBannersById/:id',async (req,res)=>{
    let data = await goodDao.queryDetailBannersById(req.params.id)
    log(`来自${req.ip.substring(req.ip.lastIndexOf(':')+1)}的请求,访问了/getDetailBannersById`);
    data.conn.end()
    res.send(data.res)
}) 

//根据id获取商品对象
goodRounter.get('/getGoodById/:id',async (req,res)=>{
    let data = await goodDao.queryGoodById(req.params.id)
    log(`来自${req.ip.substring(req.ip.lastIndexOf(':')+1)}的请求,访问了/getGoodById`);
    data.conn.end()
    res.send(data.res)
}) 

//获得所有的商品类型
goodRounter.get('/getAllType',async (req,res)=>{
    let data = await goodDao.queryAllType()
    let g = {},f = 0
    for(let i in data.res){
        if(f^1){
            if(data.res[i].typeId==8){
                g = data.res[i]
                f = 1
                continue
            }
        }else{
            data.res[i-1] = data.res[i]
        }
    }
    data.res[data.res.length-1] = g
    log(`来自${req.ip.substring(req.ip.lastIndexOf(':')+1)}的请求,访问了/getAllTypes`);
    data.conn.end()
    res.send(data.res)
})
//获得对应类型下的所有商品
goodRounter.get('/getGoodsByType/:type',async (req,res)=>{
    let data = await goodDao.queryGoodsByType(req.params.type)
    log(`来自${req.ip.substring(req.ip.lastIndexOf(':')+1)}的请求,访问了/getGoodsByType`);
    data.conn.end()
    res.send(data.res)
})
//获得对应类型下的所有商品
goodRounter.get('/getAllGoodsByType',async (req,res)=>{
    let data = await goodDao.queryAllType()
    let types = data.res;
    data.conn.end()
    let obj = {}
    for(let i in types){
        let innerData = await goodDao.queryGoodsByType(types[i].typeId)
        let goodForType = innerData.res
        innerData.conn.end()
        obj[types[i].typeId] = goodForType;
    }
    log(`来自${req.ip.substring(req.ip.lastIndexOf(':')+1)}的请求,访问了/getAllGoodsByType`);

    res.send(obj)
})

//获得对应商品的轮播图
goodRounter.get('/getBannersByGoodId/:id',async (req,res)=>{
    let data = await goodDao.queryBannersByGoodId(req.params.id)
    data.conn.end()
    log(`来自${req.ip.substring(req.ip.lastIndexOf(':')+1)}的请求,访问了/getBannersByGoodId`);
    res.send(data.res)
})
//获得所有商品
goodRounter.get('/getAllGoods',async (req,res)=>{
    let data = await goodDao.queryAllGoods()
    data.conn.end()
    log(`来自${req.ip.substring(req.ip.lastIndexOf(':')+1)}的请求,访问了/getAllGoods`);
    res.send(data.res)
})

//上传商品
goodRounter.post('/uploadGood',async (req,res)=>{
    let data = await goodDao.addNewGood(req.body.goodName,req.body.goodIcon,req.body.goodDesc,req.body.goodPrice,req.body.goodDiscount,req.body.goodDetailDesc,req.body.goodType,req.body.goodSpec)
    data.conn.end()
    let data2 = await goodDao.getLastedId()
    data2.conn.end()
    let id = data2.res[0].max
    console.log(id)
    if(req.body.bannerList.length>0){
        for(let i in req.body.bannerList){
            let inner = await goodDao.addNewBanner(id,req.body.bannerList[i])
            inner.conn.end()
        }
    }
    if(req.body.detailBannerList.length>0){
        for(let i in req.body.detailBannerList){
            let inner = await goodDao.addNewDetailBanner(id,req.body.detailBannerList[i])
            inner.conn.end()
        }
    }
    log(`来自${req.ip.substring(req.ip.lastIndexOf(':')+1)}的请求,访问了/uploadGood`);
    res.send(data.res)
})
/**
 * 上传新的商品类型
 */
goodRounter.post('/uploadType/:type',async (req,res)=>{
    let data = await goodDao.addNewType(req.params.type)
    data.conn.end()
    log(`来自${req.ip.substring(req.ip.lastIndexOf(':')+1)}的请求,访问了/uploadType`);
    res.send(data.res)
})
/**
 * 根据商品id删除商品
 */
goodRounter.post('/deleteGoodById/:goodId',async (req,res)=>{
    let data2 = await goodDao.delAllBannerById(req.params.goodId)
    data2.conn.end()
    let data = await goodDao.delGoodById(req.params.goodId)
    data.conn.end()
    log(`来自${req.ip.substring(req.ip.lastIndexOf(':')+1)}的请求,访问了/deleteGoodById`);
    res.send(data.res)
})

/**
 * 根据商品id修改商品
 */
goodRounter.post('/updateGood',async (req,res)=>{
    let data = await goodDao.updateGood(req.body.goodId,req.body.goodName,req.body.goodPrice,req.body.goodDiscount,req.body.goodDetailDesc,req.body.goodDesc,req.body.goodType,req.body.goodSpec)
    data.conn.end()
    log(`来自${req.ip.substring(req.ip.lastIndexOf(':')+1)}的请求,访问了/updateGood`);
    res.send(data.res)
})

/**
 * 获得所有商品的销量排行,根据年月
 */
goodRounter.get('/getGoodRank/:year/:month',async (req,res)=>{
    let data = await goodDao.getGoodRank(req.params.year,req.params.month)
    data.conn.end()
    log(`来自${req.ip.substring(req.ip.lastIndexOf(':')+1)}的请求,访问了/getGoodRank/`);
    res.send(data.res)
})
/**
 * 记录商品的销量
 */
goodRounter.post('/updateRanks',async (req,res)=>{
    let goods = req.body.detail
    for(let i in goods){
        let good = goods[i]
        let data = await goodDao.updateRankRecord(good.goodId,'YEAR(CURDATE())','MONTH(CURDATE())',good.goodCount)
        data.conn.end()
        if(data.res.affectedRows<1){
            let dataI = await goodDao.addRankRecord(good.goodId,good.goodName,good.goodCount)
            dataI.conn.end()
        }
    }
    log(`来自${req.ip.substring(req.ip.lastIndexOf(':')+1)}的请求,访问了/updateRanks`);
    res.send('ok')
})
module.exports = goodRounter
